<?php
require_once ('db.php');

$id = isset($_GET['id'])?$_GET['id']:0;

if (isset($_POST['email']) && !empty($_POST['email'])) {
  $uid = $_POST['id'];
  unset($_POST['cpassword']);
  unset($_POST['submitUsers']);
  unset($_POST['id']);
  if($_POST['password']== '**********' ) {
    unset($_POST['password']);
  } else {
    $_POST['password'] = md5($_POST['password']);
  }

  $q = '';
  foreach ($_POST as $colName => $colValue) {
    pr("`$colName`='$colValue', ");
    $q .= "`$colName`='$colValue', ";
  }
  $q = rtrim($q, ' ,');
  $where = " `id`='$uid'";
  echo $updateQuery = " UPDATE `users` SET  $q WHERE $where";
  mysql_query($updateQuery) or die(mysql_error());
  if (mysql_affected_rows() > 0) {
    echo "<h2>User Updated successfully</h2>";
    $url = 'http://localhost/nb158e/sql_new/select_user_search.php';
    //header("location:$url");
    //exit;
  }
}

###Select Users from Database####3
$selQuery = "SELECT * FROM `users` WHERE `id`=$id";
$selResult = mysql_query($selQuery) or die(mysql_error());
$userData = mysql_fetch_assoc($selResult);
pr($userData);
##############################
//pr($_SERVER);
?>

<form name="users" method="post" action="">

  <input type="hidden" name="id"
  value="<?=$userData['id']?>" >

  <table border="1" width="80%" align="center">
    <thead>
      <tr>
        <th colspan="2">Please fill the details</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Name :</td>
        <td>
        <input type="text" required name="uname"
        value="<?=$userData['uname']?>" >
        </td>
      </tr>
      <tr>
        <td>Email :</td>
        <td>
        <input type="email" required name="email"
        value="<?=$userData['email']?>" >
        </td>
      </tr>
      <tr>
        <td>Password :</td><td>
        <input type="password" autocomplete="off" required name="password" value="**********">
        </td>
      </tr>
      <tr>
        <td>Confirm Password :</td>
        <td>
        <input type="password" required name="cpassword" value="**********">
        </td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>
        <input type="submit" name="submitUsers" value="I am done!!">
        </td>
      </tr>
    </tbody>
  </table>
</form>
